<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Log;  
class ProcessPodcast implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
        private $taskId;

    /**
     * Create a new job instance.
     */
   public function __construct()  
{  
    // 假设我们有一个唯一的任务标识符  
   // $this->taskId = 'unique_task_id_'.time();  
    $this->taskId = 'unique_task_id_1';  
}  
  
public function handle()  
{  
    if ($this->lockTask()) {  
        // 处理任务逻辑  
           Log::info('队列执行.处理逻辑');  
           
          // 业务处理解锁
          
          if(date('d')%2){
              $this->releaseLock();  
          }
        // 完成后释放锁  
    
    } else {  
        // 任务已被锁定，跳过或记录日志  
           Log::info('任务已被锁定，跳过或记录日');  
    }  
}  
  
protected function lockTask()  
{  
    // 尝试在 Redis 中设置锁  
    // 这里只是一个示例，实际实现可能更复杂  
    return Redis::setnx("lock:{$this->taskId}", true);  
}  
  
protected function releaseLock()  
{  
    // 释放锁  
    Redis::del("lock:{$this->taskId}");  
}
}
